"use strict";
cc._RF.push(module, 'd146dF3yVlCOLTdDlXQXIQl', 'GridsLib');
// script/Game/Libs/GridsLib.ts

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridsLib = exports.DirectionMode = exports.Direction = exports.CardType = void 0;
//卡牌类型
var ResLoader_1 = require("../../Core/Libs/ResLoader");
var CardLib_1 = require("./CardLib");
var CardType;
(function (CardType) {
    CardType[CardType["Hero"] = 1] = "Hero";
    CardType[CardType["Boss"] = 2] = "Boss";
    CardType[CardType["Monster"] = 3] = "Monster";
    CardType[CardType["Health"] = 4] = "Health";
    CardType[CardType["Skill"] = 5] = "Skill";
    CardType[CardType["Treasure"] = 6] = "Treasure";
    CardType[CardType["Trap"] = 7] = "Trap";
    CardType[CardType["Shield"] = 8] = "Shield";
    CardType[CardType["Coin"] = 9] = "Coin";
})(CardType = exports.CardType || (exports.CardType = {}));
//移动方向
var Direction;
(function (Direction) {
    Direction[Direction["UP"] = 1] = "UP";
    Direction[Direction["Right"] = 2] = "Right";
    Direction[Direction["Down"] = 3] = "Down";
    Direction[Direction["Left"] = 4] = "Left";
})(Direction = exports.Direction || (exports.Direction = {}));
var DirectionMode;
(function (DirectionMode) {
    DirectionMode[DirectionMode["Forward"] = 1] = "Forward";
    DirectionMode[DirectionMode["Behind"] = 2] = "Behind";
    DirectionMode[DirectionMode["Clockwise"] = 3] = "Clockwise";
    DirectionMode[DirectionMode["AntiClockwise"] = 4] = "AntiClockwise";
})(DirectionMode = exports.DirectionMode || (exports.DirectionMode = {}));
var GridsLib = /** @class */ (function () {
    function GridsLib(wrapNode) {
        this.allGrids = new Map();
        this.itemNodeAttr = {
            width: 220,
            height: 300,
            spacingRight: 20,
            spacingBottom: 20
        };
        this.readyMoveGirdIndexMap = new Map();
        this.wrapNode = wrapNode;
    }
    //设置格子属性
    GridsLib.prototype.setGridItemAttr = function (width, height, spacingRight, spacingBottom) {
        if (spacingRight === void 0) { spacingRight = 20; }
        if (spacingBottom === void 0) { spacingBottom = 20; }
        this.itemNodeAttr.width = width;
        this.itemNodeAttr.height = height;
        this.itemNodeAttr.spacingRight = spacingRight;
        this.itemNodeAttr.spacingBottom = spacingBottom;
    };
    //生成格子
    GridsLib.prototype.generateGrids = function (rows, cells) {
        return __awaiter(this, void 0, void 0, function () {
            var i, k, itemNode, positionX, positionY, index;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        //重新定义卡牌高宽
                        this.itemNodeAttr.width = (this.wrapNode.width - this.itemNodeAttr.spacingRight * (cells - 1)) / cells;
                        this.itemNodeAttr.height = (this.wrapNode.height - this.itemNodeAttr.spacingBottom * (rows - 1)) / rows;
                        i = 0;
                        _a.label = 1;
                    case 1:
                        if (!(i < rows)) return [3 /*break*/, 6];
                        this.allGrids[i] = [];
                        k = 0;
                        _a.label = 2;
                    case 2:
                        if (!(k < cells)) return [3 /*break*/, 5];
                        return [4 /*yield*/, this.newGridItem()];
                    case 3:
                        itemNode = _a.sent();
                        positionX = -this.wrapNode.width / 2 + this.itemNodeAttr.width / 2, positionY = -this.wrapNode.height / 2 + this.itemNodeAttr.height / 2;
                        //
                        if (i > 0) {
                            positionY += i * this.itemNodeAttr.height + i * this.itemNodeAttr.spacingBottom;
                        }
                        if (k > 0) {
                            positionX += k * this.itemNodeAttr.width + k * this.itemNodeAttr.spacingRight;
                        }
                        itemNode.setPosition(positionX, positionY * -1);
                        this.wrapNode.addChild(itemNode);
                        index = i + "-" + k;
                        this.allGrids.set(index, {
                            positionX: itemNode.x,
                            positionY: itemNode.y,
                            node: itemNode,
                            cardInfo: null,
                            index: index,
                            vec2: cc.v2(i, k)
                        });
                        _a.label = 4;
                    case 4:
                        k++;
                        return [3 /*break*/, 2];
                    case 5:
                        i++;
                        return [3 /*break*/, 1];
                    case 6: return [2 /*return*/];
                }
            });
        });
    };
    GridsLib.prototype.newGridItem = function () {
        return __awaiter(this, void 0, Promise, function () {
            var prefabAsset, cardNode;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, ResLoader_1.default.load("prefab/card", cc.Prefab)];
                    case 1:
                        prefabAsset = _a.sent();
                        cardNode = cc.instantiate(prefabAsset);
                        cardNode.getComponent(CardLib_1.default).init(this.itemNodeAttr.width, this.itemNodeAttr.height);
                        //默认先隐藏
                        cardNode.active = false;
                        return [2 /*return*/, cardNode];
                }
            });
        });
    };
    GridsLib.prototype.generateSpriteFrame = function (r, g, b, w, h) {
        var texture = new cc.Texture2D();
        var spriteFrame = new cc.SpriteFrame();
        var imgData = new Uint8Array(4);
        for (var j = 0; j < 4; j += 4) {
            imgData[j] = r; // r
            imgData[j + 1] = g; // g
            imgData[j + 2] = b; // b
            imgData[j + 3] = 255; // a
        }
        texture.initWithData(imgData, cc.Texture2D.PixelFormat.RGBA8888, 1, 1);
        spriteFrame.setTexture(texture);
        spriteFrame.setRect(cc.rect(0, 0, w, h));
        return spriteFrame;
    };
    GridsLib.prototype.getAllGrids = function () {
        return this.allGrids;
    };
    //设置卡牌
    GridsLib.prototype.setGridsCard = function (cards) {
        return __awaiter(this, void 0, void 0, function () {
            var allGridsArray, _i, allGridsArray_1, gridInfo;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (this.allGrids.size == 0) {
                            return [2 /*return*/];
                        }
                        allGridsArray = Array.from(this.allGrids.values());
                        _i = 0, allGridsArray_1 = allGridsArray;
                        _a.label = 1;
                    case 1:
                        if (!(_i < allGridsArray_1.length)) return [3 /*break*/, 4];
                        gridInfo = allGridsArray_1[_i];
                        return [4 /*yield*/, this.setCardOnGrid(cards.shift(), gridInfo)];
                    case 2:
                        _a.sent();
                        _a.label = 3;
                    case 3:
                        _i++;
                        return [3 /*break*/, 1];
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    //设置单张卡在指定坐标上
    GridsLib.prototype.setCardOnGrid = function (cardInfo, grid, asyncTween) {
        if (asyncTween === void 0) { asyncTween = false; }
        return __awaiter(this, void 0, void 0, function () {
            var cardScript, d, tween;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!cardInfo) {
                            return [2 /*return*/];
                        }
                        cardScript = grid.node.getComponent(CardLib_1.default);
                        return [4 /*yield*/, cardScript.setCardAttr(cardInfo)];
                    case 1:
                        _a.sent();
                        grid.node.active = true;
                        grid.cardInfo = cardInfo;
                        d = 0.1;
                        tween = cc.tween(grid.node).sequence(cc.tween().to(d, { scaleX: 0, scaleY: 1, skewX: 0, skewY: 2 }), cc.callFunc(function () {
                            cardScript.cardFront.active = true;
                            cardScript.cardBack.active = false;
                            grid.node.skewY = -2;
                        }), cc.tween().to(d, { scaleX: 1, scaleY: 1, skewX: 0, skewY: 0 }));
                        if (!asyncTween) return [3 /*break*/, 2];
                        tween.start();
                        return [3 /*break*/, 4];
                    case 2: return [4 /*yield*/, new Promise(function (resolve) {
                            tween.call(resolve).start();
                        })];
                    case 3:
                        _a.sent();
                        _a.label = 4;
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    //注册节点事件
    GridsLib.prototype.registerClickEvent = function (callback, bind) {
        this.allGrids.forEach(function (grid) {
            grid.node.on(cc.Node.EventType.TOUCH_END, callback, bind);
        });
    };
    //通过卡牌类型 获取块信息
    GridsLib.prototype.getGridInfoByCardType = function (type) {
        var result = [];
        this.allGrids.forEach(function (info) {
            if (info.cardInfo && info.cardInfo.type == type) {
                result.push(info);
            }
        });
        return result;
    };
    GridsLib.prototype.getGridInfoByIndex = function (index) {
        return this.allGrids.get(index) || null;
    };
    GridsLib.prototype.getGridInfoByDirection = function (index, direction, mode) {
        if (mode === void 0) { mode = DirectionMode.Forward; }
        direction = this.changeDirection(direction, mode);
        return this.allGrids.get(this.getDirectionIndex(index, direction)) || null;
    };
    GridsLib.prototype.getGridInfoByNode = function (node) {
        var result = null;
        this.allGrids.forEach(function (info) {
            if (info.node.uuid == node.uuid) {
                result = info;
                return false;
            }
        });
        return result;
    };
    //获取一个方向上的所有grid
    GridsLib.prototype.getAllGridsInfoByDirection = function (index, direction, mode) {
        if (mode === void 0) { mode = DirectionMode.Forward; }
        direction = this.changeDirection(direction, mode);
        var result = [];
        while (true) {
            var gridInfo = this.allGrids.get(this.getDirectionIndex(index, direction));
            if (!gridInfo) {
                break;
            }
            result.push(gridInfo);
            index = gridInfo.index;
        }
        return result;
    };
    //获取方向的其他方向
    GridsLib.prototype.changeDirection = function (direction, mode) {
        if (mode == DirectionMode.Forward) {
            return direction;
        }
        //当前方向的反方向
        if (mode == DirectionMode.Behind) {
            switch (direction) {
                case Direction.UP:
                    return Direction.Down;
                case Direction.Left:
                    return Direction.Right;
                case Direction.Right:
                    return Direction.Left;
                case Direction.Down:
                    return Direction.UP;
            }
        }
        //当前方向的顺时针方向 / 逆时针方向
        if (mode == DirectionMode.Clockwise || mode == DirectionMode.AntiClockwise) {
            if (mode == DirectionMode.Clockwise) {
                direction += 1;
            }
            //逆时针
            else {
                direction -= 1;
            }
            if (direction < 1) {
                direction = 4;
            }
            else if (direction > 4) {
                direction = 1;
            }
            return direction;
        }
    };
    //获取指定方向的索引
    GridsLib.prototype.getDirectionIndex = function (index, direction) {
        var curIndex = index.split("-").map(Number);
        switch (direction) {
            case Direction.UP:
                curIndex[0] -= 1;
                break;
            case Direction.Down:
                curIndex[0] += 1;
                break;
            case Direction.Left:
                curIndex[1] -= 1;
                break;
            case Direction.Right:
                curIndex[1] += 1;
                break;
        }
        return curIndex.join("-");
    };
    //注册准备移动的队列
    GridsLib.prototype.joinMoveQueue = function (fromIndex, toIndex) {
        this.readyMoveGirdIndexMap.set(toIndex, fromIndex);
    };
    GridsLib.prototype.startMove = function (moveList) {
        return __awaiter(this, void 0, Promise, function () {
            var tweenArray;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (moveList.length == 0) {
                            return [2 /*return*/, null];
                        }
                        tweenArray = [];
                        moveList.forEach(function (fromGridInfo, k) {
                            //第一个跳过并隐藏
                            if (k == 0) {
                                fromGridInfo.node.active = false;
                                return true;
                            }
                            //目标节点是 当前索引前一个
                            var targetGridInfo = moveList[k - 1];
                            var tween = new Promise(function (resolve) {
                                cc.tween(fromGridInfo.node).to(0.3, { x: targetGridInfo.positionX, y: targetGridInfo.positionY }).call(function () {
                                    resolve();
                                }).start();
                            });
                            tweenArray.push(tween);
                        });
                        return [4 /*yield*/, Promise.all(tweenArray).then(function () {
                                //移动后覆盖格子值
                                moveList.forEach(function (targetGridInfo, k) {
                                    var fromGridInfo;
                                    //最后一个的节点  用第一个节点补充
                                    if (k == moveList.length - 1) {
                                        fromGridInfo = moveList[0];
                                        //这里把第一个节点的位置 移动到最后一个节点的位置上
                                        fromGridInfo.node.x = targetGridInfo.positionX;
                                        fromGridInfo.node.y = targetGridInfo.positionY;
                                    }
                                    else {
                                        fromGridInfo = moveList[k + 1];
                                    }
                                    //从缓存副本取值
                                    _this.allGrids.set(targetGridInfo.index, {
                                        positionX: targetGridInfo.positionX,
                                        positionY: targetGridInfo.positionY,
                                        node: fromGridInfo.node,
                                        cardInfo: fromGridInfo.cardInfo,
                                        index: targetGridInfo.index,
                                        vec2: targetGridInfo.vec2
                                    });
                                });
                                //返回需要补卡的节点
                                return _this.allGrids.get(moveList[moveList.length - 1].index);
                            })];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    return GridsLib;
}());
exports.GridsLib = GridsLib;

cc._RF.pop();